﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using ET.DBCommon.Model;
using MySql.Data.MySqlClient;

namespace ET.DBCommon.DAL
{
    /// <summary>
    /// s时序图数据库操作类 Add by zzg
    /// </summary>
    public class SequenceDiagramDAL
    {
        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="sequencediagram"></param>
        /// <returns></returns>
        public static bool InsertSequenceDiagram(SequenceDiagramModel sequencediagram)
        {
            string sql = @"INSERT INTO `sequencediagram`
                            (`UserID`,
                            `OperID`,
                            `OperTime`,
                            `virtualTime`,
                            `DeviceTypeID`,
                            `DeviceID`,
                            `Parameters`,
                            `Remark`,
                            `SystemType`,
                            `ExamresultsStepID`,
                            `AudioFile`,
                            `IsEffective`,`EndVirtualTime`
                            ,`IsMain`)
                            VALUES
                            (@UserID,
                            @OperID,
                            now(6),
                            @VirtualTime,
                            @DeviceTypeID,
                            @DeviceID,
                            @Parameters,
                            @Remark,
                            @SystemType,
                            @ExamresultsStepID,
                            @AudioFile,
                            @IsEffective,
                            now(6),
                            @IsMain)";
            MySqlParameter[] parms = {
                                    new MySqlParameter("@UserID", sequencediagram.UserID ),
                                    new MySqlParameter("@OperID", sequencediagram.OperID),
                                    //new MySqlParameter("@OperTime", sequencediagram.OperTime),
                                    new MySqlParameter("@VirtualTime", sequencediagram.VirtualTime),

                                    new MySqlParameter("@DeviceTypeID", sequencediagram.DeviceTypeID ),
                                    new MySqlParameter("@DeviceID", sequencediagram.DeviceID),
                                    new MySqlParameter("@Parameters", string.IsNullOrWhiteSpace(sequencediagram.Parameters)?"":sequencediagram.Parameters),
                                    new MySqlParameter("@Remark", string.IsNullOrWhiteSpace(sequencediagram.Remark)?"":sequencediagram.Remark),
                                    new MySqlParameter("@SystemType", sequencediagram.SystemType ),
                                    new MySqlParameter("@IsEffective", sequencediagram.IsEffective),
                                    new MySqlParameter("@AudioFile", sequencediagram.AudioFile),
                                    new MySqlParameter("@ExamresultsStepID", sequencediagram.ExamresultsStepID),
                                    new MySqlParameter("@EndVirtualTime", sequencediagram.EndVirtualTime),
                                    new MySqlParameter("@IsMain", sequencediagram.IsMain)
            };
            return DbHelper.ExecuteNonQuery(sql, parms) > 0;
        }

        /// <summary>
        /// 查询考试内容
        /// </summary>
        /// <param name="ExamresultsStepID"></param>
        /// <returns></returns>
        public static List<SequenceDiagramModel> GetSequenceDiagram(int ExamresultsStepID)
        {
            string sql = "SELECT * FROM sequencediagram where ExamresultsStepID = " + ExamresultsStepID;
            sql += " order by ID";
            DataSet ds = DbHelper.ExecuteDataset(sql);
            List<SequenceDiagramModel> list = new List<SequenceDiagramModel>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                list = ToObservableCollection<SequenceDiagramModel>(dt).ToList();
            }
            return list;
        }

        /// <summary>
        /// 把DataTable转换为任何模型
        /// 调用方法：IList<模型> myStruct = ContentCenterStyle<模型>.ConvertToModel(表格);
        /// </summary>
        public static IList<T> ToObservableCollection<T>(DataTable dt) where T : class, new()
        {            // 定义集合    
            IList<T> ts = new List<T>();
            //  ObservableCollection<T> ts = new ObservableCollection<T>();
            // 获得此模型的类型   
            Type type = typeof(T);
            string tempName = "";

            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();
                // 获得此模型的公共属性      
                PropertyInfo[] propertys = t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;  // 检查DataTable是否包含此列    

                    if (dt.Columns.Contains(tempName))
                    {
                        // 判断此属性是否有Setter      
                        if (!pi.CanWrite) continue;

                        object value = dr[tempName];
                        if (value != DBNull.Value)
                            pi.SetValue(t, value, null);
                    }
                }
                ts.Add(t);
            }
            return ts;
        }
        /// <summary>
        /// 获取绘制时序图需要的步骤
        /// </summary>
        /// <param name="testQuestionID"></param>
        /// <returns></returns>
        public static List<StepSequenceDiagramModel> GetStepDesListByQuestionID(int testQuestionID)
        {
            string sql = @"select testquestiontask.TestQuestionTaskID,TestQuestionTaskStepID,StepDescribe,StepContent,TaskType,DevType,DevID,StepID, testquestiontask.IsMain 
from testquestiontask inner join testquestiontaskstep on testquestiontaskstep.TestQuestionTaskID = testquestiontask.TestQuestionTaskID 
inner join testquestion on testquestion.TestQuestionID = testquestiontask.TestQuestionID where  testquestion.TestQuestionID = " + testQuestionID+
" order by testquestiontask.UIOrder,testquestiontaskstep.Order;";
            //string sql = "select TestQuestionTaskStepID,StepDescribe,TaskType,DevType,DevID from testquestiontask inner join testquestiontaskstep on testquestiontaskstep.TestQuestionTaskID = testquestiontask.TestQuestionTaskID inner join testquestion on testquestion.TestQuestionID = testquestiontask.TestQuestionID where testquestion.TestQuestionID = " + testQuestionID;//联查
            DataSet ds = DbHelper.ExecuteDataset(sql);

            List<StepSequenceDiagramModel> lst = new List<StepSequenceDiagramModel>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    StepSequenceDiagramModel info = GetStepSequenceDiagramModel(dt.Rows[i]);
                    lst.Add(info);
                }
            }
            return lst;
        }
        private static StepSequenceDiagramModel GetStepSequenceDiagramModel(DataRow dr)
        {
            StepSequenceDiagramModel info = new StepSequenceDiagramModel();
            info.TestQuestionTaskID = Convert.ToInt32(dr["TestQuestionTaskID"]);
            info.TestQuestionTaskStepID = Convert.ToInt32(dr["TestQuestionTaskStepID"]);
            info.StepDescribe = dr["StepDescribe"].ToString();
            info.StepContent = dr["StepContent"].ToString();
            info.TaskType = Convert.ToInt32(dr["TaskType"]);
            info.DevType = Convert.ToInt32(dr["DevType"]);
            info.DevID = dr["DevID"].ToString();
            info.StepID = Convert.ToInt32(dr["StepID"]);
            if (dr["IsMain"] != DBNull.Value)
                info.IsMain = bool.Parse(dr["IsMain"].ToString());
            return info;
        }
    }
}
